package com.example.arithmeticleetcode.leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @program: arithmetic-leetcode
 * @description: 1455. 检查单词是否为句中其他单词的前缀
 * @author: FangZhen
 * @create: 2020-05-25 09:53
 **/
public class Demo23 {

    public static void main(String[] args) {
        String sentence = "i love eating burger", searchWord = "burg";
        System.out.println(isPrefixOfWord(sentence, searchWord));

        String sentence1 = "this problem is an easy problem", searchWord1 = "pro";
        System.out.println(isPrefixOfWord(sentence1, searchWord1));

        String sentence2 = "i am tired", searchWord2 = "you";
        System.out.println(isPrefixOfWord(sentence2, searchWord2));

        String sentence3 = "i use triple pillow", searchWord3 = "pill";
        System.out.println(isPrefixOfWord(sentence3, searchWord3));

        String sentence4 = "hello from the other side", searchWord4 = "they";
        System.out.println(isPrefixOfWord(sentence4, searchWord4));
    }

    public static int isPrefixOfWord(String sentence, String searchWord) {
        String[] strings = sentence.split(" ");
        for (int i = 0, len = strings.length; i < len; i++) {
            if (strings[i].startsWith(searchWord)) {
                return i + 1;
            }
        }
        return -1;
    }
}
